import requests
import json
import pandas as pd
from datetime import datetime
import os
columns = ["站名", "时间", "水位(m)", "流量(m3/s)", "超警戒(m)"]
filts = ["stnm", "tm", "z", "q", "cmpwrz"]
# admauth=360100 鄱阳湖

def  save(response, filepath, filename):
    data_list = list(json.loads(response.text)['data'])
    # print(data_list)
    df = pd.DataFrame(data_list)[filts]
    df.columns = columns
    df.fillna("", inplace=True)
    print(df.head(5))
    #
    #
    #
    file_path = os.path.join(filepath, filename)
    df.to_excel(file_path, index=False)
    print(f"文件已保存到: {file_path}")
    return file_path

def crawl( filepath = "./save"):
    # name = [360100, ]
    headers = {
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Referer': 'http://weixin.jxsswj.cn/jxsw/v2/riverList.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
        'X-Requested-With': 'XMLHttpRequest',
    }

    response1 = requests.get('http://weixin.jxsswj.cn/jxsw/data/realtime/river/riverResultList.json', headers=headers, verify=False)


    headers2 = {
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Referer': 'http://weixin.jxsswj.cn/jxsw/v2/riverList.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
        'X-Requested-With': 'XMLHttpRequest',
    }

    response2 = requests.get('http://weixin.jxsswj.cn/jxsw/data/realtime/river/riverResultList.json?admauth=360100', headers=headers, verify=False)

    today_date = datetime.now().strftime("%Y%m%d_%H%M")
    filename1 = f"江西水文重点江河站水情-{today_date}.xlsx"
    filename2 = f"鄱阳湖江河水情-{today_date}.xlsx"


    save(response1, filepath, filename1)
    save(response2, filepath, filename2)

    return filename1, filename2

def crawl_jiangxishuiwen():
    headers = {
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Referer': 'http://weixin.jxsswj.cn/jxsw/v2/riverList.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
        'X-Requested-With': 'XMLHttpRequest',
    }
    
    response1 = requests.get('http://weixin.jxsswj.cn/jxsw/data/realtime/river/riverResultList.json', headers=headers, verify=False)
    data = list(json.loads(response1.text)['data'])
    response1.close()
    return data

def crawl_poyanghu():
    headers2 = {
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Referer': 'http://weixin.jxsswj.cn/jxsw/v2/riverList.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
        'X-Requested-With': 'XMLHttpRequest',
    }

    response2 = requests.get('http://weixin.jxsswj.cn/jxsw/data/realtime/river/riverResultList.json?admauth=360100', headers=headers2, verify=False)
    data = list(json.loads(response2.text)['data'])
    response2.close()
    return data

def crawl2( filepath = "./save"):
    # name = [360100, ]
    headers = {
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Referer': 'http://weixin.jxsswj.cn/jxsw/v2/riverList.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
        'X-Requested-With': 'XMLHttpRequest',
    }

    response1 = requests.get('http://weixin.jxsswj.cn/jxsw/data/realtime/river/riverResultList.json', headers=headers, verify=False)


    headers2 = {
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Referer': 'http://weixin.jxsswj.cn/jxsw/v2/riverList.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
        'X-Requested-With': 'XMLHttpRequest',
    }

    response2 = requests.get('http://weixin.jxsswj.cn/jxsw/data/realtime/river/riverResultList.json?admauth=360100', headers=headers, verify=False)

    today_date = datetime.now().strftime("%Y%m%d_%H%M")
    filename1 = f"江西水文重点江河站水情.xlsx"
    filename2 = f"鄱阳湖江河水情.xlsx"


    save(response1, filepath, filename1)
    save(response2, filepath, filename2)

    return filename1, filename2
